package com.clps.rulesregulations.service.impl;

import com.clps.rulesregulations.dao.ResourceDao;
import com.clps.rulesregulations.domain.Resource;
import com.clps.rulesregulations.service.ResourceService;
import com.clps.rulesregulations.util.ResourceTree;
import com.clps.rulesregulations.util.TreeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 资源逻辑处理
 *
 * @author zhangchao
 * @date 2018-11-06
 */
@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private ResourceDao resourceDao;
    @Override
    public PageInfo<Resource> findResourcesByPage(Map<String, Object> condition) {
        Integer pageNum = 1;
        try {
            pageNum = (Integer) condition.get("pageNum");
            pageNum = pageNum==0?1:pageNum;
        }catch (Exception e){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,10);
        List<Resource> resources = resourceDao.findResourcesByCondition(condition);
        return new PageInfo<Resource>(resources);
    }

    @Override
    public List<Resource> findAll() {
        Map<String,Object> condition = new HashMap<>();
        return resourceDao.findAll();
    }

    @Override
    @Cacheable(cacheNames="resources",key="#map['userId'].toString()+#map['type']")
    public List<Resource> loadUserResources(Map<String, Object> map) {
        return resourceDao.loadUserResources(map);
    }

    @Override
    public List<Resource> findResourcesByRoleId(Integer roleId) {
        return resourceDao.findResourcesByRoleId(roleId);
    }

    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @Override
    public void addResource(Resource resource) {
        resourceDao.addResource(resource);
    }


    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @Override
    public void delResource(Integer resourceId) {
        resourceDao.delResource(resourceId);
    }

    @Override
    public void deleteAll(String[] ArrayDATA_IDS) {
        resourceDao.deleteAll(ArrayDATA_IDS);
    }

    @Override
    public Resource findResourceById(Integer resourceId) {
        return resourceDao.findResourceById(resourceId);
    }

    @Override
    public Resource findResourceByUrl(String url) {
        return resourceDao.findResourceByUrl(url);
    }

    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @Override
    public void updateResource(Resource resource) {
        resourceDao.updateResource(resource);
    }

    @Override
    public List<ResourceTree> findAllAsTree() {
        LinkedList<ResourceTree> resourceTrees = resourceDao.findAllAsTree();
        ResourceTree root = new ResourceTree(0,null);
        root = TreeUtil.getTree(root,resourceTrees);
        return TreeUtil.createDepthFirstTraversalList(root);
    }

    @Override
    public List<Resource> selectAll() {
        List<Resource> cnAreas = resourceDao.findMenus();
        return cnAreas;
    }

}
